$(function() {
	var QianYueGrid = $("#QianYueGrid");
	// 初始化
	QianYueGrid.datagrid({
		url: 'zs-qianyue-list/list',
		fit: true,
		border:false,
		rownumbers: true,
		pagination: true, // 是否显示分页栏
		fitColumns: true,
		columns: [
			[{
				field: 'input_stu_name',
				title: '学生姓名',
				align: 'center',
				width: 100
			},{
				field: 'user_name',
				title: '咨询师姓名',
				align: 'center',
				width: 100
			},{
				field: 'images_path',
				title: '是否有相关的凭证',
				align: 'center',
				width: 100,
				formatter : function(value, row, index) {
					if (value != null) {
						var bts = [];
						bts.push('<a style="color:black;text-decoration: none;" href="'+ 'upload/'+row.images_path +'"target="_blank" data-index="' + index + '" class="ctr preview">预览</a>');
						bts.push('<a style="color:black;text-decoration: none;" href="'+ 'download/'+row.images_id +'" data-index="' + index + '" class="ctr download">下载</a>');
						return bts.join('');
					}else{
						return "否";
					}
				}
				
			},{
				field: 'sign_sch_length',
				title: '学制',
				align: 'center',
				width: 100
			},{
				field: 'sign_time',
				title: '签约时间',
				sortable:true,
				align: 'center',
				width: 100
			}, {
				field: 'add_plus',
				title: '备注',
				align: 'center',
				width: 100
			}, {
				field: 'ac',
				title: '操作',
				align: 'center',
				width: 150,
				formatter: function(value, row, index) {
					var bts = [];
					bts.push('<a data-index="' + index + '" class="ctr edit">查看详情</a>');
					bts.push('<a data-id="' + row.sign_id + '" data-index="' + index + '" class="ctr delete">删除</a>');
					return bts.join('');
				}
			}]
		],
		toolbar: [{
			iconCls: 'icon-add',
			text: '添加',
			handler: function() {
				AddQianYue();
			}
		}]
	});
	
	// 据id获取input
	var signSchLength = $("#signSchLength");
	var signTime = $("#signTime");
	// 搜索按钮事件
	$("#signSearch").on('click', function() {
		QianYueGrid.datagrid("load", {
			signSchLength: "%" + signSchLength.val() + "%",
			signTime: "%" + signTime.val() + "%"
		});
	});

	// 据id获取表单对象
	var form = $("#signSearchForm");
	// 重置按钮事件
	$("#signReset").on('click', function() {
		form.form('clear');
		// 清除查询参数数据
		QianYueGrid.datagrid("load", {});
	});

	/**
	 * 编辑，删除
	 */
	var gridPanel = QianYueGrid.datagrid("getPanel");
	gridPanel.on("click", "a.edit", function() {
		var index = this.dataset.index;
		// 如果只有下标，没有id的情况，要获取id
		var rows = QianYueGrid.datagrid("getRows");
		// 同下标获取对应行的数据对象
		var id = rows[index].sign_id;
		AddQianYue(id);
	}).on("click", "a.delete", function() {
		// 获取data-xxx绑定的标签属性数据
		var index = this.dataset.index;
		var id = this.dataset.id;
		$.messager.confirm("提示", "您确定要删除此数据?", function(r) {
			// 点击了确定按钮，r返回的结果为true
			if (r) {
				$.get("zs-qianyue-list/delete/" + id, function(rs) {
					if (rs.success) {
						// 刷新表格
						QianYueGrid.datagrid("reload");
					} else {
						$.message.alert("提示", rs.message);
					}
				});
			}
		});
	});
	
	function AddQianYue(id) {
		var dialog = $("<div/>").dialog({
			href: 'zs-qianyue-list/zs-add-qianyue' + (id ? ("/" + id) : ""),
			title: '签约',
			height: '400',
			width: '560',
			modal: true,
			onClose: function() {
				$(this).dialog("destroy");
			},
			buttons: [{
				iconCls: 'icon-save',
				text: '添加',
				handler : function() {
					$("#signForm").form('submit', {
						url : 'zs-qianyue-list/save',
						data : $("#signForm").serialize(),
						success : function(success) {
							if (success) {
								// 刷新数据表格
								QianYueGrid.datagrid("reload");
								// 关闭窗口
								dialog.dialog("close");
								$.messager.alert("提示", "上传成功！");
							} else {
								$.messager.alert("提示", success.message);
							}
						}
					});
				}
			}, {
				iconCls: 'icon-clear',
				text: '取消',
				handler: function() {
					dialog.dialog("close");
				}
			}]
		});
	}
});